package com.tianqu.tms.www.user.controller;

import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.entity.User;
import com.tianqu.tms.www.car.action.CarInnerListTopAction;
import com.tianqu.tms.www.user.action.UserUDTopListAction;

/**
 * 用户信息列表页面
 * 
 * @author tianshufu
 * 
 */
public class UserUDTopListController extends TmsController {

	private Logger logger = Logger.getLogger(UserUDTopListController.class);
	
	public static final String PAGING = "paging";

	@Override
	public void execute(UserContext uc) throws Exception {

		logger.debug(this.getClass().getName() + "#execute was called.");

		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();

			TmsUserProfile userprofile = uc.getUserProfile();

			// 获取一个List
			User user = User.getInstance();
			if (uc.getAttribute(UserUDTopListAction.PAGENO) != null) {
				user.setPageNo(Integer.parseInt((String) uc
						.getAttribute(UserUDTopListAction.PAGENO)));
			}
			if (uc.getAttribute(UserUDTopListAction.MAXINDEX) != null) {
				user.setMaxIndex(Integer.parseInt((String) uc
						.getAttribute(UserUDTopListAction.MAXINDEX)));
			}
			if (uc.getAttribute(UserUDTopListAction.SORTKEY) != null) {
				user.setSortKey(Integer.parseInt((String) uc
						.getAttribute(UserUDTopListAction.SORTKEY)));
			}
			if (uc.getAttribute(UserUDTopListAction.ASC) != null) {
				user.setAsc((new Boolean((String) uc
						.getAttribute(UserUDTopListAction.ASC)))
						.booleanValue());
			}	
			List<User> list = user.getAllUserInfo(userprofile.getUserClass(),
					userprofile.getUniqueId());

			uc.setAttribute("userList", list);

			logger
					.debug(this.getClass().getName()
							+ "#doAction was completed.");
			uc.setAttribute(PAGING, user);
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

	}

}
